/*   Save the info below in data.txt
Monta Vista's Mascot is a:
A.  Dragon
B.  Bull
C.  Hummingbird
D.  Rutabaga
C
Monta Vista High School is located in:
A.  Cupertino, CA
B.  Las Vegas, NV
C.  New York, NY
D.  Nome, AK
A
My favorite class is:
A.  Anything but Java
B.  Geometry
C.  PE
D.  Biology
C
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;

public class GameHelp extends JApplet
{
	private CardLayout cards;
	private Container c;
	private GamePanel gpanel;
	private RadioPanel rpanel;
	public final int numberofquestions = 3;

	public void init ( )
	{
		c = this.getContentPane();
		cards = new CardLayout();
		c.setLayout(cards);
		c.setBackground(Color.black);
   
		gpanel = new GamePanel();
      	gpanel.setBackground(Color.white);
        	c.add(gpanel,"GAME PANEL");

		rpanel = new RadioPanel();
      	rpanel.setBackground(Color.gray);
        	c.add(rpanel, "RADIO PANEL");
	}

	class GamePanel extends JPanel implements KeyListener
	{
		private Font myFont;
         
		public GamePanel ( )
		{
			addKeyListener(this);
		}
		
		public void paintComponent (Graphics g)
		{
			super.paintComponent(g);
			myFont = new Font("Arial", Font.BOLD, 36);
			g.setFont(myFont);
			g.setColor(Color.black);
			g.drawString("This is the Game Panel", 40, 300);
			myFont = new Font("Arial", Font.BOLD, 20);
			g.setFont(myFont);
			g.drawString("Press the space bar to continue", 80, 350);
			this.requestFocus();
		}

		public void keyPressed (KeyEvent e)
		{
			int value = e.getKeyCode();
			if (value == KeyEvent.VK_SPACE)
			{
				int choice = (int)(Math.random() * numberofquestions);
				rpanel.loadQuestion(choice);
				cards.show(c, "RADIO PANEL");
			}
		}

		public void keyTyped (KeyEvent e)   {}

		public void keyReleased (KeyEvent e)   {}
	}

	class RadioPanel extends JPanel implements ActionListener, KeyListener
	{
		private JRadioButton A, B, C, D, placeHolder;
		private JLabel prompt, result;
		private File afile;
		private String [] question;
		private String [][] answer;
		private String [] correctanswer;
		private int currentquestion;
		private boolean moveon;

		public RadioPanel ( )
		{
			this.setLayout(new GridLayout(6,1));
			addKeyListener(this);
			loadDataFromFile();
			currentquestion = 0;
			moveon = false;

			prompt = new JLabel("Answer this question.", JLabel.CENTER);
			prompt.setForeground(Color.white);
			prompt.setBackground(Color.black);
			prompt.setOpaque(true);
			this.add(prompt);
     	 
			ButtonGroup answerGroup = new ButtonGroup();
      
			A = new JRadioButton("First Choice");
			answerGroup.add(A);
			A.addActionListener(this);
			this.add(A);

			B = new JRadioButton("Second Choice");
			answerGroup.add(B);
			B.addActionListener(this);
			this.add(B);

			C = new JRadioButton("Third Choice");
			answerGroup.add(C);
			C.addActionListener(this);
			this.add(C);

			D = new JRadioButton("Fourth Choice");
			answerGroup.add(D);
			D.addActionListener(this);
			this.add(D);

			placeHolder = new JRadioButton("You cannot see me . . .");
			answerGroup.add(placeHolder);
			placeHolder.setSelected(true);
			
			result = new JLabel("Choose one of the four answers in response to the prompt above.", JLabel.CENTER);
			result.setForeground(Color.white);
			result.setBackground(Color.black);
			result.setOpaque(true);
			this.add(result);
		}

		public void loadDataFromFile ( )
		{
			afile = new File("data.txt");
			Scanner fromfile = null;
			question = new String [numberofquestions];
			answer = new String [numberofquestions][4];
			correctanswer = new String [numberofquestions];
			String aline;
			try
			{
				fromfile = new Scanner(afile);
			}
			catch (FileNotFoundException e)
			{
				System.out.println("Cannot find file");
				System.exit(1);
			}
			int i = 0;
			while (fromfile.hasNext())
			{
				aline = fromfile.nextLine();
				question[i] = aline;
				System.out.println(aline);
				for (int j = 0; j < 4; j++)
				{
					aline = fromfile.nextLine();
					answer[i][j] = aline;
					System.out.println(aline);
				}
				aline = fromfile.nextLine();
				correctanswer[i] = aline;
				System.out.println(aline);
				i++;
			}
		}

		public void loadQuestion (int choice)
		{
			System.out.println(choice);
			currentquestion = choice;
			prompt.setText(question[choice]);
			A.setText(answer[choice][0]);
			B.setText(answer[choice][1]);
			C.setText(answer[choice][2]);
			D.setText(answer[choice][3]);
			result.setBackground(Color.black);
			result.setText("Choose one of the four answers in response to the prompt above.");
			placeHolder.setSelected(true);
			A.setEnabled(true);
			B.setEnabled(true);
			C.setEnabled(true);
			D.setEnabled(true);
			moveon = false;
		}

		public void paintComponent (Graphics g)
		{
			this.requestFocus();
		}
   
		public void actionPerformed (ActionEvent evt)
		{
			int value = 0;
			if (A.isSelected())
			{
				value = 0;
				B.setEnabled(false);
				C.setEnabled(false);
				D.setEnabled(false);
			}
			else if (B.isSelected())
			{
				value = 1;
				A.setEnabled(false);
				C.setEnabled(false);
				D.setEnabled(false);
			}
			else if (C.isSelected())
			{
				value = 2;
				A.setEnabled(false);
				B.setEnabled(false);
				D.setEnabled(false);
			}
			else if (D.isSelected())
			{
				value = 3;
				A.setEnabled(false);
				B.setEnabled(false);
				C.setEnabled(false);
			}
			if(answer[currentquestion][value].substring(0,1).equals(correctanswer[currentquestion]))
			{
				result.setBackground(Color.green);
				result.setText("That is correct!  Press the space bar to return to the game.");
			}
			else
			{
				result.setBackground(Color.red);
				result.setText("Sorry, the correct answer was " +  correctanswer[currentquestion] + 
					".  Press the space bar to return to the game.");
			}
			moveon = true;
			this.requestFocus();
		}

		public void keyPressed (KeyEvent e)
		{
			int value = e.getKeyCode();
			if (value == KeyEvent.VK_SPACE && moveon)
			{
				cards.show(c, "GAME PANEL");
			}
		}

		public void keyTyped (KeyEvent e)   {}

		public void keyReleased (KeyEvent e)   {}
	}
}